<?php
namespace tp5_lib\alipay\utility;

define("AOP_SDK_WORK_DIR",env("runtime_path"));
require "../extend/alipay/AopSdk.php";

use AopClient;
use tp5_lib\account\extension\PaymentMethod;
use tp5_lib\install\logic\DynamicConfig;

/**
 * @property AopClient $client
 */
class Alipay implements PaymentMethod
{
    public $sandbox=true;
    public function __construct($sandbox=null)
    {
        if($sandbox !== null){
            $this->sandbox = $sandbox;
        }
        try {
            $values=DynamicConfig::alloc("modules")->read("alipay_config");
        } catch (\Exception $exce) {
            $config = include __DIR__ . "/../module/module.php";
            $config = $config["config"];
            foreach($config as $key => $item){
                $values[$key]=$item["value"];
            }
        }
        $aop=new AopClient();
        $aop->format = "json";
        $aop->charset = "GBK";
        if($this->sandbox){
            $aop->gatewayUrl = $values["sandbox-gatewayUrl"];
            $aop->appId = $values["sandbox-appId"];
            $aop->rsaPrivateKey = $values["sandbox-rsaPrivateKey"];
            $aop->signType= $values["sandbox-signType"];
            $aop->alipayrsaPublicKey = $values["sandbox-alipayrsaPublicKey"];
        }
        else{
            $aop->gatewayUrl = $values["gatewayUrl"];
            $aop->appId = $values["appId"];
            $aop->rsaPrivateKey = $values["rsaPrivateKey"];
            $aop->signType= $values["signType"];
            $aop->alipayrsaPublicKey = $values["alipayrsaPublicKey"];
        }
        $this->client=$aop;
    }
    public function getOAuthUrl($redirect_uri)
    {
        $host = $this->sandbox ? "https://openauth.alipaydev.com" : "https://openauth.alipay.com";
        $redirect_uri=urlencode($redirect_uri);
        return $host."/oauth2/publicAppAuthorize.htm?app_id={$this->client->appId}&scope=auth_user,auth_base&redirect_uri=$redirect_uri";
    }
    /**
     * 预创建
     * @param string $no 订单编号
     * @param float $amount 订单金额
     * @param string $title 标题
     * @param string $content 内容
     */
    public function precreate($no,$amount,$title,$content="")
    {
        $request = new \AlipayTradePrecreateRequest();
        $request->setReturnUrl("http://tp5.1620428040.xyz/alipay/index/onreturn");
        $request->setNotifyUrl("http://tp5.1620428040.xyz/alipay/index/onnotify");
        $request->setBizContent(json_encode([
            "out_trade_no"=>$no,
            "total_amount"=>$amount,
            "subject"=>$title,
        ]));
        $response = $this->client->execute($request);
        $resultCode = $response->alipay_trade_precreate_response->code;
        if(empty($resultCode) || $resultCode != 10000){
            $this->error="创建订单失败";
            return false;
        }
        require_once '../extend/phpqrcode.php';
        header("Content-type: image/png");
        \QRcode::png($response->alipay_trade_precreate_response->qr_code);
        exit;
    }
    /**
     * 手机支付
     */
    public function mobile($no,$amount,$title,$content="")
    {
        $request = new \AlipayTradePrecreateRequest();
        $request->setReturnUrl("http://tp5.1620428040.xyz/alipay/index/onreturn");
        $request->setNotifyUrl("http://tp5.1620428040.xyz/alipay/index/onnotify");
        $request->setBizContent(json_encode([
            "out_trade_no"=>$no,
            "total_amount"=>$amount,
            "subject"=>$title,
        ]));
        return $this->client->sdkExecute($request);
        // $response = $this->client->execute($request);
        // $resultCode = $response->alipay_trade_precreate_response->code;
        // if(empty($resultCode) || $resultCode != 10000){
        //     $this->error="创建订单失败";
        //     return false;
        // }
        // require_once '../extend/phpqrcode.php';
        // header("Content-type: image/png");
        // \QRcode::png($response->alipay_trade_precreate_response->qr_code);
        // exit;
    }
}
